// pages/component/handle/handle.js
const key = require('../../../key.js')
const amap = require('../../../utils/amap-wx.js')
const app = getApp()
const mqtt = require('../../../utils/mqtt.min.js')


Page({

   /**
    * 页面的初始数据
    */
   data: {
      StatusBar: app.globalData.StatusBar,
      CustomBar: app.globalData.CustomBar,
      list: [{
         title: '用户信息',
         img: 'https://image.weilanwl.com/color2.0/plugin/sylb2244.jpg',
         url: '/indexes/indexes',
         type:'getUserInfo',
         bind:'bindGetUserInfo'
      },
      {
         title: '定位',
         img: 'https://image.weilanwl.com/color2.0/plugin/wdh2236.jpg',
         url: '/animation/animation',
         type:'',
         tap:'getloca'
      },
      {
         title: '获取手机号',
         img: 'https://image.weilanwl.com/color2.0/plugin/qpct2148.jpg',
         url: '/drawer/drawer',
         type:'getPhoneNumber',
         bind:'',
         num:'getPhoneNumber'
      },
      {
         title: '用户id',
         img: 'https://image.weilanwl.com/color2.0/plugin/qpczdh2307.jpg',
         url: '/verticalnav/verticalnav',
         type:'',
         bind:''
      }
      ]
   },
   toCalender(){
      wx.redirectTo({
         url: '../sigle/move',
      })
   },
   getPhoneNumber(e){
    
      wx.login({
         success: res => {
            if (res.code) {
               console.log(res.code)
               wx.request({
                  url: 'http://192.168.2.17:8093/getRoomInfo',
                  method:'POST',
                  data:{
                     code: res.code,
                     iv: e.detail.iv,
                     encrypted: e.detail.encryptedData
                  },
                  header:{
                     'content-type': 'application/x-www-form-urlencoded'
                  },
                  success:res=>{
                     console.log('res',res)
                  },
                  fail:err=>{
                     console.log('err',err)
                  }
               })
               let data_ ={
                  code:res.code,
                  iv:e.detail.iv,
                  encryptedData:e.detail.encryptedData
               }
               console.log('da',data_)
            }
         }
      })
      wx.checkSession({
         success() { },
         fail() {
            wx.login() // 重新登录
         }
      })
   },
   getloca(){
      console.log('location')
      let myAmap = new amap.AMapWX({ key: key.mapKey})
      myAmap.getRegeo({
         success:res=>{
            console.log('res',res)
         },
         fail:err=>{
            console.log('err',err)
         }
      })
      wx.getLocation({
         type: 'wgs84',
         success(res) {
            console.log(res)
            const latitude = res.latitude
            const longitude = res.longitude
            const speed = res.speed
            const accuracy = res.accuracy
         }
      })
   },
   bindGetUserInfo(e) {
      wx.vibrateShort({})
      const accountInfo = wx.getAccountInfoSync()
      console.log(accountInfo.miniProgram.appId)
      wx.login({
         success: res => {
            if (res.code) {
               console.log(res.code)
               wx.getUserInfo({
                  success: e => {
                    console.log(e)
                  },
                  fail: f => {
                     console.log('fail,login_fail')
                     console.log
                     wx.showToast({
                        title: '失败',
                     })
                  }
               })
            }
         }
      })
      wx.checkSession({
         success() { },
         fail() {
            wx.login() // 重新登录
         }
      })
   },

   mqtt(){
      console.log('点击成功')
      var client = mqtt.connect('wx://119.3.206.48:8083/mqtt', {
         clientId: Math.random(0,99),
         username: 'weixin',
         password: 'weixin'
      })
      client.on('connect', _ => {
         console.log('mqtt连接成功')
         client.subscribe('/hotel/#', (err) => {
            if (!err) {
               //   client.publish('/hotel/123', 'Hello ttsj_weapp')
               console.log('订阅成功')
            }
         })
      })
      client.on('error', error => {
         console.log(error)
      })
      client.on('message', (topic, message) => {
         // message is Buffer
         console.log('收到消息：' + message.toString())
         // client.end()
      })
   },
   onLoad: function (options) {

   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   }
})